package com.woniuxy.woniubookmanager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.Manager;
import com.woniuxy.woniubookmanager.entity.Role;
import com.woniuxy.woniubookmanager.result.ResponseResult;
import com.woniuxy.woniubookmanager.service.ManagerService;
import com.woniuxy.woniubookmanager.utils.JwtUtils;
import com.woniuxy.woniubookmanager.vo.AccountVo;
import com.woniuxy.woniubookmanager.vo.ManagerVo;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 内部员工信息 前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@RestController
@RequestMapping("/manager")
//@CrossOrigin  //设置允许跨域 本类中的方法 都允许跨域
public class ManagerController {
    /*
        restFul风格的操作方式
        添加   ： post     PostMapping
        修改   ： put      PutMapping
        删除   ： delete   DeleteMapping
        查询   ： get      GetMapping
     */
    @Autowired
    ManagerService managerService ;

    @PostMapping("/login")
    //@CrossOrigin  //设置允许跨域
    public ResponseResult<Manager> login(@RequestBody AccountVo accountVo){
        ResponseResult<Manager> result = null;
        try {
            Manager manager = managerService.getManagerByName(accountVo.getUsername());
            if (manager == null){
                result = new ResponseResult<>(201,"账户不存在！");
            }else {
                if (manager.getUserPassword().equals(accountVo.getPassword())) {
                    //登录成功
                    String token = JwtUtils.createToken(accountVo.getUsername(),24*60*60*1000);
                    result = new ResponseResult<>(200,"OK",manager, token);
                }else {
                    result = new ResponseResult<>(202,"密码错误！");
                }
            }
        }catch (RuntimeException e) {
            result = new ResponseResult<>(500,"系统错误！");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getAccount")
    public ResponseResult<String> getAccount(String token){
        ResponseResult<String> result = null;
        try {
            String account = managerService.getAccount(token);
            result = new ResponseResult<>(200,"OK",account);
        }catch (RuntimeException e){
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/managerList")
    public ResponseResult<Page<Manager>> list(@RequestParam(value = "now",defaultValue = "1") Integer now ,
                                           @RequestParam(value = "size",defaultValue = "2")Integer size,
                                           String mname){
        ResponseResult<Page<Manager>> result = null;
        try {
            Page<Manager> page = managerService.getManagerByPage(now,size,mname);
            result = new ResponseResult<>(200,"ok",page);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败!");
            e.printStackTrace();
        }
        return result;
    }

    @PutMapping("/modifyManager")
    public ResponseResult<Void> modifyManager(@RequestBody ManagerVo managerVo){
        ResponseResult<Void> result = null;
        try {
            managerVo.setUpdateTime(new Date());
            managerService.modifyManager(managerVo);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"修改失败!");
            e.printStackTrace();
        }
        return result;
    }

}